package org.neu.onlineparadise.service.impl;

import org.neu.onlineparadise.entity.User;
import org.neu.onlineparadise.mapper.UserMapper;
import org.neu.onlineparadise.service.UserService;
import org.neu.onlineparadise.utils.BCryptUtil;
import org.neu.onlineparadise.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    //查找
    @Override
    public User findByName(String username) {
        User user = mapper.findByName(username);
        return user;
    }

    //注册
    @Override
    public void register(String username, String password) {
        //使用bcrypt加密密码
        String stringMBCrypt= BCryptUtil.getpw(password);
        mapper.register(username,stringMBCrypt);
    }

    @Override
    public void updateUserInfo(User user) {
        mapper.updateUserInfo(user);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id=(Integer) map.get("id");
        String bcryptPwd=BCryptUtil.getpw(newPwd);
        mapper.updatePwd(bcryptPwd,id);

     }

    @Override
    public void updateAvatar(String avatarURL) {
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id=(Integer) map.get("id");
        mapper.updateAvatar(avatarURL,id);
    }
}
